function getHttp(a, data, callback) {
    $.ajax({
        url: sdk.baseIP + a,
        data: data,
        dataType: 'jsonp',
        success: function(d, r, x) {
            callback(d, r, x);
        }
    });
}

function toBack() {
    window.history.back();
}
//监听登录选择
var user = {
    '4': {
        name: '企业用户',
        register: true,
        path: 'zxw/company/xq/release_demand',
        yjfk: 'zxw/company/ts/complaint_admin'
    },
    '5': {
        name: '个人用户',
        register: true
    },
    '1': {
        name: '银行用户',
        register: false,
        path: 'zxw/jrjg/bankDbsx/wdxq',
        yjfk: 'zxw/jrjg/bankDbsx/tsgl',
        cpdel: 'mh/app/product/proInfo',
        clName: '#bankJgDel'
    },
    '3': {
        name: '政府部门',
        path: 'zxw/gov/app/qyzhcsp?tname=qyzcsp',
        yjfk: 'zxw/gov/app/tscl?tname=tscl',
        register: false
    },
    '6': {
        name: '保险用户',
        register: false,
        path: 'zxw/bxjg/wdsp',
        cpdel: 'mh/app/v2/pro/getBxjgProductInfoOne',
        clName: '#bxJgDel'
    },
    '8': {
        name: '证券用户',
        register: false,
        path: 'zxw/zqjg/wdsp',
        cpdel: 'mh/app/v2/pro/getZqProductInfoViewOne',
        clName: '#zqJgDel'
    },
    '2': {
        name: '担保公司',
        register: false,
        path: 'zxw/jrjg/dbgsDbsx/auditList',
        cpdel: 'mh/app/v2/pro/getDbjgProductInfoOne',
        clName: '#dbJgDel'
    },
    '9': {
        name: '投资机构',
        register: false
    },
    '7': {
        name: '金融专员',
        path: 'zxw/jrzy/index/finance_jrzy',
        register: false
    }
};
layui.use(['form', 'layer', 'element'], function() {
    var form = layui.form;
    var layer = layui.layer;
    var element = layui.element;
    // 监听底部下拉选择
    form.on('select(urlTo)', function(data) {
        console.log(data);
        if (data.value) {
            window.open(data.value)
        }
    });
    // 改变验证码
    function changeValidateImg() {
        $('#imgYzm').attr('src', '').attr('src', sdk.companyIp +
            'zxw/company/login/getRandomValidateCode?r=' + Math.random());
        form.val("formVcyk", {
            validateCode: ''
        });
    };
    // 登录渲染角色类型
    function loginMain(typeId) {
        if (typeId == 'qy') {
            $('#bklwxo').text('企业登录');
            $('#zwrkoe').show();
            // $('#getkdh').attr('href', sdk.companyIp + 'mh/register.html');
            // $('#xhpoee').attr('href', sdk.baseIP + 'zxw/company/qy/forget_pwd');
            // $('#xhpoee1').attr('href', sdk.baseIP + 'zxw/company/qy/forget_username');
        } else if (typeId == 'jg') {
            $('#bklwxo').text('机构登录');
            $('#zwrkoe').hide();
        } else if (typeId == 'zf') {
            $('#bklwxo').text('政府登录');
            $('#zwrkoe').show();
        }
        layer.open({
            type: 1,
            title: false,
            shadeClose: true,
            content: $('#layerVcyk'),
            success: function(layero, index) {
                changeValidateImg();
                $('#imgYzm').click(function() {
                    changeValidateImg()
                });
                form.on('submit(formGohk)', function(d) {
                    var rjzpIndex = layer.load(2);
                    var data = d.field;
                    var acc = data.account;
                    var psw = data.password;
                    data.account = btoa(acc);
                    data.password = btoa(psw);
                    setTimeout(function() {
                        layer.close(rjzpIndex);
                    }, 5000)
                    $.ajax({
                        type: 'post',
                        data: data,
                        url: sdk.companyIp +
                            'zxw/company/login/loginCheck',
                        success: function(d) {
                            layer.close(rjzpIndex);
                            consoleMain();
                            changeValidateImg();
                            window.location.href =
                              //  'https://ggzy.jz.gov.cn:8606/zxw/jrjg/bankDbsx/wdxq';
                                'https://ggzy.jz.gov.cn:8606/zxw/bxjg/bxgsDbsx/dxbxxqListData';
                            layer.close(index);
                            layer.msg('登录成功', {
                                icon: 6,
                                closeBtn: 1,
                                time: 3000
                            });
                        }
                    });
                });
            },
            end: function() {
                form.val("formVcyk", {
                    account: '',
                    password: '',
                    validateCode: ''
                });
            }
        });
    };

    // 渲染登录页面
    $('#loginId').click(function() {
        var code = 'jg';
        var loginSvkc = function(typeId) {
            if ($('#layerVcyk').length == 0) {
                $.ajax({
                    url: 'login2.html',
                    dataType: 'html',
                    success: function(html) {
                        $('body').append(html);
                        loginMain(typeId);
                    }
                });
            } else {
                loginMain(typeId)
            }
        };
        if (code) {
            loginSvkc(code);
        }
    });
   

    // 跳转注册
    $('#toRegister').click(function() {
        location.href = sdk.companyIp + 'mh/register.html'
    })
    form.verify({
        pass: [/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/, '密码必须由6到18位字母和数字组成']
    });
    // 格式化输入文本
    function filterHTMLTag(msg) {
        var msg = msg.replace(/<[^>]+>|&[^>]+;/g, ''); //去除HTML Tag
        msg = msg.replace(/[|]*\n/, '') //去除行尾空格
        msg = msg.replace(/&npsp;/ig, ''); //去掉npsp
        return msg;
    }
    $('input,textarea').on('input', function() {
        $(this).val(filterHTMLTag($(this).val()))
    });
    // 根据地址参数触发修改密码
    if (sdk.getURLVar('type') === 'mpwd') {
        changePassword();
        history.pushState('', '', 'index.html');
    }

    // 修改密码
    function changePassword() {
        $.ajax({
            url: 'modify_pwd.html',
            dataType: 'html',
            success: function(d) {
                $('body').append(d);
                layer.open({
                    type: 1,
                    title: false,
                    shadeClose: true,
                    content: $('#nhnvve'),
                    success: function(layero, index) {
                        form.on('submit(formTwlh)', function(d) {
                            var data = d.field;
                            if (data.oldPassword === data.newPassword) {
                                layer.msg('新密码和旧密码不能相同', {
                                    icon: 5
                                });
                                return false;
                            }
                            if (data.newPassword !== data.newPasswordRepeat) {
                                layer.msg('两次输入的密码不一致', {
                                    icon: 5
                                });
                                return false;
                            }
                            data.oldPassword = btoa(data.oldPassword);
                            data.newPassword = btoa(data.newPassword);
                            $.ajax({
                                type: 'post',
                                data: data,
                                url: sdk.baseIP +
                                    'zxw/company/core/changePassword',
                                success: function(d) {
                                    if (d.code === '00') {
                                        layer.close(index);
                                        layer.msg('修改成功', {
                                            icon: 6,
                                            closeBtn: 1,
                                            time: 3000
                                        });
                                        location.href = sdk.baseIP +
                                            'zxw/company/login/loginout'
                                    } else {
                                        layer.msg(d.msg, {
                                            icon: 5,
                                            closeBtn: 1,
                                            time: 3000
                                        });
                                    }
                                }
                            })
                        })
                    },
                    end: function() {
                        form.val('formPyzj', {
                            oldPassword: '',
                            newPassword: '',
                            newPasswordRepeat: ''
                        })
                    }
                })
            }
        })
    }
    // 跳转到工作台
    $('#bankName').click(function() {
        window.location.href =
            'https://ggzy.jz.gov.cn:8606/zxw/jrjg/bankDbsx/wdxq';
    });


    // 签退
    $('#logoutBtn').click(function() {
        var layer = layui.layer;
        layer.confirm('你确定要退出吗?', {
            icon: 0,
            title: '提示'
        }, function(index) {
            layer.close(index);
            sessionStorage.setItem('baohan_info', '');
            location.href = sdk.baseIP + 'zxw/company/login/loginout';
        });
    });

    // 个人信息
    function consoleMain() {
        $.ajax({
            type: 'post',
            url: sdk.companyIp + 'zxw/company/login/getLoginFlag',
            dataType: 'json',
            success: function(userinfo) {
                if (userinfo.loginFlag == 'true') {
                    $('#isLogin').removeClass('noShow');
                    $('#noLogin').addClass('noShow');
                    $('#bankName').text(userinfo.userName);
                } else {
                    var userName = sessionStorage.getItem('baohan_info');
                    if (userName) {
                        $('#noLogin').addClass('noShow');
                        $('#isLogin').removeClass('noShow');
                        $('#bankName').text('保函用户');
                    } else {
                        $('#noLogin').removeClass('noShow');
                        $('#isLogin').addClass('noShow');
                    }
                }
            },
            error: function(err) {}
        });
    }
    consoleMain();
    renderDianzibaohan();
});
